import kotlin.concurrent.thread

fun main(args: Array<String>) {
    /**
     * "$JAVA_HOME/bin/java"
     * -classpath "$ASPECTJ_HOME/lib/aspectjtools.jar:$JAVA_HOME/lib/tools.jar:$CLASSPATH"
     * -Xmx64M org.aspectj.tools.ajc.Main "$@"
     */
    /**
     * "$JAVA_HOME/bin/java"
     * -classpath "$ASPECTJ_HOME/lib/aspectjtools.jar:$JAVA_HOME/lib/tools.jar:$CLASSPATH"
     * -Xmx64M org.aspectj.tools.ajc.Main "$@"
     */
    val cmds = arrayListOf(
            /**
             * JAVA_HOME  可能没设置，这里强制设置下
             */
            "export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home",

            /**
             * ajc 需要的CLASSPATH
             */
            "export CLASSPATH=/Users/rover12421/Downloads/aspectj-1.8.13/lib/aspectjtools.jar:\$JAVA_HOME/lib/tools.jar:\$CLASSPATH",

            "export CLASSPATH=\$CLASSPATH:\$JAVA_HOME/jre/lib/rt.jar",

            /**
             *  添加jfx支持
             */
            "export CLASSPATH=\$CLASSPATH:\$JAVA_HOME/jre/lib/jfxrt.jar",
            "export CLASSPATH=\$CLASSPATH:\$JAVA_HOME/jre/lib/ext/jfxrt.jar"
    )
//    addClassPath(appleJarDir)

//    classPath.forEach {
//        println("add classPath $it")
//        cmds.add("export CLASSPATH=\$CLASSPATH:$it")
//    }

    cmds.add("export CLASSPATH=\$CLASSPATH:/Volumes/Code/Rover12421/Zkm/ant/ant.jar")

    cmds.add("java -classpath \$CLASSPATH -Xmx1024m org.aspectj.tools.ajc.Main " +
            "-7 " +
            "-injars /Volumes/Code/Rover12421/Zkm/lib/ZKM.jar " +
            "-injars /Users/rover12421/Downloads/aspectj-1.8.13/lib/aspectjrt.jar " +
            "-sourceroots /Volumes/Code/Rover12421/Zkm/ajc " +
            "-outjar /Volumes/Code/Rover12421/Zkm/lib/ZKM-ajc.jar")

    exec(cmds)

}

/**
 * 执行系统命令
 * 默认 shell 使用 bash
 */
fun exec(cmd: Collection<String>,
         shell: String = "bash",
         printOut: Boolean = true,
         printErr: Boolean = true,
         retOut: Boolean = true,
         retErr: Boolean = true
): String {
    val rt = Runtime.getRuntime()
    val process = rt.exec(shell)

    thread {
        process.outputStream.use {
            it.bufferedWriter().use { bufferedWriter ->
                cmd.forEach {
                    println(it)
                    bufferedWriter.write(it)
                    bufferedWriter.newLine()
                }
            }
        }
    }

    val sb = StringBuilder()
    thread {
        process.inputStream.use {
            it.bufferedReader().use { ins ->
                try {
                    while (true) {
                        val line = ins.readLine() ?: break

                        if (retOut) {
                            sb.append(line)
                        }
                        if (printOut) {
                            println(line)
                        }
                    }
                } catch (ignore: Throwable) {
                }
            }
        }
    }

    thread {
        process.errorStream.use {
            it.bufferedReader().use { ins ->
                try {
                    while (true) {
                        val line = ins.readLine() ?: break

                        if (retErr) {
                            sb.append(line)
                        }
                        if (printErr) {
                            println(line)
                        }
                    }
                } catch (ignore: Throwable) {
                }
            }
        }
    }

    process.waitFor()

    return sb.toString()
}

